package com.yeswater.multithread.chapter2.test2_2.test5;

/**
 * author : yeswater
 * date : 2023/10/20
 *
 * 同一个类中的 synchronized 代码块锁的是同一个对象
 */

public class Run {

    volatile private String str = "";

    public static void main(String[] args) {
        ObjService service = new ObjService();
        ThreadA a = new ThreadA(service);
        a.setName("a");
        a.start();

        ThreadB b = new ThreadB(service);
        b.setName("b");
        b.start();
    }

    private static class ThreadA extends Thread {
        private final ObjService service;

        public ThreadA(ObjService service) {
            this.service = service;
        }

        @Override
        public void run() {
            service.serviceMethodA();
        }
    }

    private static class ThreadB extends Thread {
        private final ObjService service;

        public ThreadB(ObjService service) {
            this.service = service;
        }

        @Override
        public void run() {
            service.serviceMethodB();
        }
    }
}
